package cn.xyh.service.impl;

import cn.xyh.dao.MenuWriteMapper;
import cn.xyh.exception.MenuException;
import cn.xyh.model.Menu;
import cn.xyh.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl implements IMenuService {
    @Autowired
    private MenuWriteMapper menuWriteMapper;


    /**
     * 新增
     */
    @Override
    public Integer insertMenu(Menu entity) throws MenuException {

        Integer result = 0;

        try {
            result = menuWriteMapper.insertMenu(entity);

        } catch (Exception e) {
            throw new MenuException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 删除多个
     */
    @Override
    public Integer delMenuByIds(String entityIds) throws MenuException {
        Integer result = 0;
        try {
            String[] _entityIds = entityIds.split(",");
            result = menuWriteMapper.delMenuByIds(_entityIds);

        } catch (Exception e) {
            throw new MenuException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 删除
     */
    @Override
    public Integer delMenu(long id) throws MenuException {
        Integer result = 0;
        try {

            result = menuWriteMapper.delMenu(id);

        } catch (Exception e) {
            throw new MenuException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }


    /**
     * 修改
     */
    @Override
    public Integer updateMenu(Menu entity) throws MenuException {
        int result = 0;

        try {

            result = menuWriteMapper.updateMenu(entity);

        } catch (Exception e) {
            throw new MenuException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 根据ID查找对象
     */
    @Override
    public Menu getMenuById(long menuId) throws MenuException {

        Menu entity = null;
        try {
            entity = menuWriteMapper.getMenuById(menuId);


        } catch (Exception e) {
            throw new MenuException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return entity;
    }

    /**
     * 根据Code查找对象
     */
    @Override
    public Menu getMenuByCode(String code) throws MenuException {

        Menu entity = null;
        try {
            entity = menuWriteMapper.getMenuByCode(code);

        } catch (Exception e) {
            throw new MenuException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return entity;
    }

    /**
     * 根据name查找对象
     */
    @Override
    public Menu getMenuByName(String name) throws MenuException {

        Menu entity = null;
        try {
            entity = menuWriteMapper.getMenuByName(name);

        } catch (Exception e) {
            throw new MenuException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return entity;
    }

    /**
     * 根据条件查找列表总数
     */
    @Override
    public Integer getMenuCount(Menu entity) throws MenuException {
        int result = 0;
        try {
            result = menuWriteMapper.getMenuCount(entity);

        } catch (Exception e) {
            throw new MenuException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 根据条件查找列表
     *
     * @param entity      实体对象
     * @param startRownum 开始条数
     * @param endRownum   结束条数
     * @param sortField   排序字段
     * @param sortOrder   正序/倒序
     */
    @Override
    public List<Menu> getMenuList(Menu entity) throws MenuException {


        List<Menu> list = null;

        try {
            list = menuWriteMapper.getMenuList(entity);

        } catch (Exception e) {
            throw new MenuException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return list;
    }
}

